//
//  ViewController.m
//  UnitDemo
//
//  Created by wangqinglu on 15/11/9.
//  Copyright © 2015年 xs. All rights reserved.
//

#import "UDViewController.h"
#import "UDLoginManager.h"

@interface UDViewController () {
//    UIButton* g_loginButton;
}
@property(nonatomic, readwrite) UIButton* loginButton;
@property(nonatomic, readwrite) UDLoginManager* loginManager;
@end

@implementation UDViewController
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void) loadView {
    [super loadView];
    UIView *v = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    v.backgroundColor = [UIColor whiteColor];
    self.view = v;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setupView];
    self.loginManager = [UDLoginManager sharedInstance];
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(receivedSignInSuccessNotification:)
     name:UDLOGINMANAGER_NOTIFICATION_SIGNIN_SUCCESS object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) setupView {
    float kTextFieldWidth = 200.0;
    float kTextFieldHeight = 44.0;
    UITextField* userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, kTextFieldWidth, kTextFieldHeight)];
    userNameTextField.placeholder = @"输入用户名";
    userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    CGPoint c = self.view.center;
    c.y -= 200.0;
    userNameTextField.center = c;
    
    UITextField* passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, kTextFieldWidth, kTextFieldHeight)];
    passwordTextField.placeholder = @"输入密码";
    passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
    c.y += kTextFieldHeight + 20.0;
    passwordTextField.center = c;
    
    UIButton* signInButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, kTextFieldWidth, kTextFieldHeight)];
    c.y += kTextFieldHeight + 20.0;
    signInButton.center = c;
    signInButton.backgroundColor = [UIColor greenColor];
    signInButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    //    signInButton.layer.borderWidth = 0.5;
    signInButton.layer.cornerRadius = 5.0;
    [signInButton setTitle:@"登录" forState:UIControlStateNormal];
    
    [signInButton addTarget:self action:@selector(pressedLoginButton:) forControlEvents:UIControlEventTouchUpInside];
    self.loginButton = signInButton;
    
    [self.view addSubview:userNameTextField];
    [self.view addSubview:passwordTextField];
    [self.view addSubview:signInButton];
}

-(void) pressedLoginButton:(id)sender {
    [self.loginManager signInWithUserName:@"wql" Password:@"123456"];
}

#pragma mark - Action
-(void) receivedSignInSuccessNotification:(NSNotification*)notification {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.loginButton setTitle:@"退出" forState:UIControlStateNormal];
        self.loginButton.backgroundColor = [UIColor redColor];
    });
}
@end
